home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.02 Feb 96 / Adding Scripts to Menus / Script Menu test project / Powering Up Code / SCDoc.cp < prev    next >
Encoding:
Text File  |  1995-04-05  |  6.0 KB  |  264 lines  |  [TEXT/MMCC]

  1. // ===========================================================================
  2. // SCDoc.cp
  3. // ===========================================================================
  4. // © 1995 James Kaput, Jeremy Roschelle SimCalc Project
  5.  
  6. #include "SCDoc.h"
  7. #include "SCPage.h"
  8.  
  9. // consts for appleEvents
  10. const DescType ae_Sort = 10000;
  11. const DescType keySortDirection = 'srtD';
  12. const DescType kSortAscending = '>   ';
  13.  
  14. SCDoc::SCDoc(LCommander    *inSuper)
  15.     : LDocument (inSuper), mSelection (nil)
  16. {
  17.     // add five pages, just for fooling around with
  18.     for (short i = 0; i < 5; ++i) new SCPage(this);
  19.     
  20.     // make us the default model
  21.     // you might not choose to do this in your app
  22.     // or you might want the frontmost doc to be the default model
  23.     SetDefaultModel(this); 
  24.     // note that because we do this, we have to override MakeSelfSpecifier
  25.     // so that we are the "null" (outermost) container
  26. }
  27.  
  28. SCDoc::~SCDoc()
  29. {
  30.     LListIterator    iter(mPageList,iterate_FromEnd);
  31.     SCPage    *page;
  32.     while(iter.Next(&page)) delete page;
  33. }
  34.  
  35. StringPtr    
  36. SCDoc::GetDescriptor(Str255 outDescriptor) const
  37. {
  38.     CopyPStr("\puntitled",outDescriptor);
  39.     return outDescriptor;
  40. }
  41.  
  42. void        
  43. SCDoc::SetSelection(SCPage *inPage)
  44. {
  45.     mSelection = inPage;
  46. }
  47.  
  48. void        
  49. SCDoc::DoSort(Boolean inAscending)
  50. {
  51.     LList            list;
  52.     LListIterator    iter(mPageList, iterate_FromStart);
  53.     SCPage        *page, *comparePage;
  54.     Str255        name, compareName;
  55.     Int32        i, count = 0, index;
  56.     
  57.     // do insertion sort
  58.     while(iter.Next(&page)) {
  59.         page->GetDescriptor(name);
  60.         index = count + 1;
  61.         for(i = 1; i <= count; ++i) {
  62.             list.FetchItemAt(i,&comparePage);
  63.             comparePage->GetDescriptor(compareName);
  64.             if (-1 == RelString(name,compareName,false,false)) {
  65.                 index = i;
  66.                 break;
  67.             }
  68.         }
  69.         list.InsertItemsAt(1,index,&page);
  70.         ++count;
  71.     }
  72.     
  73.     // copy back, reversing order if need be
  74.     mPageList.RemoveItemsAt(count,1);
  75.     LListIterator    iter2(list,iterate_FromStart );
  76.     while(iter2.Next(&page)) 
  77.         mPageList.InsertItemsAt(1,inAscending ? arrayIndex_Last : arrayIndex_First,&page);    
  78. }
  79.  
  80.  
  81. void        
  82. SCDoc::AddSubModel(LModelObject *inSubModel)
  83. {
  84.     if (SCPage::modelKind == inSubModel->GetModelKind()) {
  85.         mPageList.InsertItemsAt(1,arrayIndex_Last,&inSubModel);    
  86.     }
  87. }
  88.  
  89. void        
  90. SCDoc::RemoveSubModel(LModelObject *inSubModel)
  91. {
  92.     if (SCPage::modelKind == inSubModel->GetModelKind()) {
  93.         mPageList.Remove(inSubModel);    
  94.     }
  95. }
  96.  
  97. Int32        
  98. SCDoc::CountSubModels(DescType inModelID) const
  99. {
  100.     if (inModelID == SCPage::modelKind) return (mPageList.GetCount());
  101.     ThrowOSErr_(errAENotAnElement);
  102.     return 0; // prevent compiler warning
  103. }
  104.  
  105. Int32            
  106. SCDoc::GetPositionOfSubModel(DescType inModelID,
  107.                        const LModelObject    *inSubModel) const
  108. {
  109.     if (inModelID == SCPage::modelKind) {
  110.         return mPageList.FetchIndexOf(&inSubModel);
  111.     }
  112.     
  113.     ThrowOSErr_(errAENotAnElement);
  114.     return 0; // prevent compiler warning
  115. }
  116.  
  117.  
  118. void        
  119. SCDoc::GetSubModelByPosition(
  120.                         DescType        inModelID,
  121.                         Int32        inPosition,
  122.                         AEDesc        &outToken) const
  123. {
  124.     if (inModelID == SCPage::modelKind){
  125.         SCPage *page;
  126.         
  127.         // LList::FetchItemAt not declared const (even though we know it is).
  128.         // so we need this assignment and cast to sneak it by the compiler.
  129.         LList        *list = (LList *)&mPageList;
  130.         if (list->FetchItemAt(inPosition,&page)) {
  131.             PutInToken(page,outToken); 
  132.             return;
  133.         }
  134.     }
  135.     ThrowOSErr_(errAENotAnElement);
  136. }
  137.  
  138.                         
  139. void        
  140. SCDoc::GetSubModelByName(DescType        inModelID,
  141.                         Str255            inName,
  142.                         AEDesc            &outToken) const
  143. {
  144.     if (inModelID == SCPage::modelKind){
  145.         SCPage *page;
  146.         Str255 pageName;
  147.         LListIterator    iter(mPageList,iterate_FromStart);
  148.  
  149.         while (iter.Next(&page)) {
  150.             page->GetDescriptor(pageName);
  151.             if (EqualString(pageName,inName,false,false)) {
  152.                  PutInToken(page,outToken);
  153.                 return;
  154.             }
  155.         }
  156.     }
  157.     ThrowOSErr_(errAENotAnElement);
  158. }
  159.  
  160.                         
  161. void        
  162. SCDoc::GetSubModelByUniqueID(
  163.                         DescType        inModelID,
  164.                         const AEDesc    &inKeyData,
  165.                         AEDesc            &outToken) const
  166. {
  167.     long        id,pageID;
  168.  
  169.     UExtractFromAEDesc::TheInt32(inKeyData,id);
  170.  
  171.     if (inModelID == SCPage::modelKind){
  172.         SCPage *page;
  173.         LListIterator    iter(mPageList,iterate_FromStart);
  174.  
  175.         while (iter.Next(&page)) {
  176.             pageID = page->GetID();
  177.             if (pageID == id) {
  178.                  PutInToken(page,outToken);
  179.                 return;
  180.             }
  181.         }
  182.     }
  183.     ThrowOSErr_(errAENotAnElement);
  184. }
  185.  
  186. void    
  187. SCDoc::GetAEProperty(
  188.                     DescType        inProperty,
  189.                     const AEDesc    &inRequestedType,
  190.                     AEDesc            &outPropertyDesc) const
  191. {
  192.     switch (inProperty) {
  193.         case 'psel': // selection property
  194.             if (mSelection) mSelection->MakeSpecifier(outPropertyDesc);
  195.             break;
  196.         default:
  197.             inherited::GetAEProperty(inProperty,inRequestedType,outPropertyDesc);
  198.     }
  199.  
  200. }
  201.  
  202.  
  203. void        
  204. SCDoc::HandleAppleEvent(
  205.                     const AppleEvent    &inAppleEvent,
  206.                     AppleEvent            &outAEReply,
  207.                     AEDesc                &outResult,
  208.                     Int32                inAENumber)
  209. {
  210.     switch (inAENumber) {
  211.         case ae_CountElements:
  212.             HandleCountEvent(inAppleEvent, outAEReply, outResult);
  213.             break;
  214.         case ae_Sort:
  215.         {
  216.             StAEDescriptor    desc;
  217.             OSType        direction = kSortAscending;
  218.             desc.GetOptionalParamDesc(inAppleEvent,keySortDirection,typeEnumeration);
  219.             if (desc.mDesc.dataHandle != nil) { // check for optional parameter
  220.                 UExtractFromAEDesc::TheEnum(desc.mDesc,direction);
  221.             }
  222.             DoSort(direction == kSortAscending);
  223.             break;
  224.         }
  225.         default:
  226.             inherited::HandleAppleEvent(inAppleEvent,
  227.                                 outAEReply,
  228.                                 outResult,
  229.                                 inAENumber);
  230.             break;
  231.     }
  232. }
  233.  
  234. void    
  235. SCDoc::HandleCountEvent(const AppleEvent    &inAppleEvent,
  236.                 AppleEvent        &outAEReply,
  237.                 AEDesc        &outResult)
  238. {
  239.     OSErr        err;
  240.     DescType    descType, objectType;
  241.     long        actualSize, count;
  242.  
  243.     err  = AEGetParamPtr(&inAppleEvent, keyAEObjectClass,
  244.                         typeType,&descType,
  245.                         &objectType,
  246.                         sizeof(descType),&actualSize);   /* IM VI chap. 6 pg 75 */
  247.     FailOSErr_(err);
  248.     
  249.     count = CountSubModels(objectType);
  250.       err = AECreateDesc(typeLongInteger, (Ptr) &count, sizeof(long), &outResult);
  251.     FailOSErr_(err);
  252. }
  253.  
  254. // we need to override this to return the null container because we have set ourself to be the 
  255. // default container
  256. void
  257. SCDoc::MakeSelfSpecifier(
  258.     AEDesc    &inSuperSpecifier,
  259.     AEDesc    &outSelfSpecifier) const
  260. {
  261.     outSelfSpecifier.descriptorType = typeNull;
  262.     outSelfSpecifier.dataHandle = nil;
  263. }
  264.